package br.com.northwind.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.northwind.database.Conexao;
import br.com.northwind.exceptions.ConexaoException;
import br.com.northwind.exceptions.DriverException;
import br.com.northwind.fabricas.FabricaConexao;
import br.com.northwind.interfaces.IShippersDao;
import br.com.northwind.tabelas.Shippers;

public class ShippersDao implements IShippersDao {

	private final String SQL_INSERT = "INSERT INTO Shippers (CompanyName, Phone) VALUES (?,?)";
	private final String SQL_UPDATE = "UPDATE Shippers SET CompanyName = ?, Phone = ? WHERE ShipperId = ?";
	private final String SQL_DELETE = "DELETE FROM Shippers WHERE ShipperId = ?";
	private final String SQL_SELECT_TODOS = "SELECT * FROM Shippers";
	private final String SQL_SELECT_BY_CODIGO = "SELECT * FROM Shippers WHERE ShipperId = ?";

	private Conexao conexao;
	private PreparedStatement statement = null;
	private ResultSet rs = null;

	public ShippersDao() {
		this.conexao = FabricaConexao.criarConexao();
	}

	public ShippersDao(Conexao conexao) {
		this.conexao = conexao;
	}

	public void inserir(Shippers _shippers) throws ConexaoException,DriverException {
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_INSERT);
			
			this.statement.setString(1, _shippers.getCompanyName());
			this.statement.setString(2, _shippers.getPhone());
			
			this.statement.execute();
		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
	}

	public void editar(Shippers _shippers) throws ConexaoException,DriverException {
		PreparedStatement update = null;
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_BY_CODIGO);
			
			this.statement.setInt(1, _shippers.getShipperId());
			
			this.rs = statement.executeQuery();

			if (rs.next()) {
				update = this.conexao.getConexao().prepareStatement(this.SQL_UPDATE);

				update.setString(1, _shippers.getCompanyName());
				update.setString(2, _shippers.getPhone());

				update.execute();
				update.close();
			}
		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				if (this.rs != null) this.rs.close();
				if (update != null) update.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}

	}

	public void excluir(Integer _shipperid) throws ConexaoException,DriverException {
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_DELETE);
			
			this.statement.setInt(1, _shipperid);
			
			this.statement.execute();

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				if (this.rs != null) this.rs.close();
				
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
	}

	public List<Shippers> selecionarAll() throws ConexaoException,DriverException {
		List<Shippers> listaDeShippers = new ArrayList<Shippers>();
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_TODOS);
			this.rs = this.statement.executeQuery();

			while (rs.next()) {
				
				int ShipperId = this.rs.getInt(1);
				String CompanyName = this.rs.getString(2);
				String Phone = this.rs.getString(3);
				
				listaDeShippers.add(new Shippers(ShipperId, CompanyName, Phone));
				
			}

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
		return listaDeShippers;
	}

	public Shippers selecionarByCod(Integer _shipperid) throws ConexaoException, DriverException {
		Shippers localizado = null;
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_BY_CODIGO);
			this.statement.setInt(1, _shipperid);
			this.rs = this.statement.executeQuery();

			if (rs.next()) {
				
				int ShipperId = this.rs.getInt(1);
				String CompanyName = this.rs.getString(2);
				String Phone = this.rs.getString(3);
				
				localizado = new Shippers(ShipperId, CompanyName, Phone);
				
			}

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
		return localizado;
	}

}
